//
//  MessageViewController.m
//  MM
//
//  Created by 郑渊文 on 5/20/15.
//  Copyright (c) 2015 王晨辉. All rights reserved.
//

#import "MessageViewController.h"
#import "CommonMacro.h"
#import "MessagerDetailViewController.h"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
     UIButton *backBtn;
}
@property(nonatomic,copy)UITableView *myTable;
@property(nonatomic,copy)NSMutableArray *messageTitleArray;//设置选项
@property(nonatomic,copy)NSMutableArray *messageIdArray;//设置选项
@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    _messageTitleArray = [NSMutableArray array];
    _messageIdArray = [NSMutableArray array];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *ID = [user objectForKey:@"ID"];

    NSDictionary *param4 = @{@"userId":ID};
    
    [UserDAL getMessageCenterParams:param4 withBlock:^(NSDictionary *data, NSError *error) {
        if (data) {
            ////NSLog(@"what%@",data);
            NSArray *array = data[@"dataList"];
            for (NSDictionary *areaDic in array) {
                [_messageTitleArray addObject:areaDic[@"title"]] ;
                [_messageIdArray addObject:areaDic[@"messageId"]] ;
            }
            
            [_myTable reloadData];

        }else
        {
            alert(@"对不起，你所在的地区没有麦苗公学。");
        }
    }];
    
//  _messageTitleArray = @[@"园内公告1",@"园内公告2",@"园内公告3",@"园内公告4",@"园内公告5",@"园内公告6",@"园内公告7"];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    NSString *msg = [[NSString alloc] initWithFormat:@"您点击的是第%d个按钮!",buttonIndex];
    ////NSLog(@"msg:%@",msg);
    if (buttonIndex == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        ////NSLog(@"1111111");
        
    }else if(buttonIndex == 1){
        
        
        ////NSLog(@"2222222");
        
        
    }
    
    
}

-(void)initSubViews
{
    self.navigationItem.title = @"消息中心";
    [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#34bd81" alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];

    self.view.backgroundColor = [UIColor clearColor];

    //自定义左右按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
     
                      forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(back)
     
     forControlEvents:UIControlEventTouchUpInside];
    
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    self.navigationItem.leftBarButtonItem = menuButton;
    

    _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    [self.view addSubview:_myTable];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _messageTitleArray.count;
}


#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
    }
    cell.textLabel.text = _messageTitleArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MessagerDetailViewController *sugVC = [[MessagerDetailViewController alloc]init];
    sugVC.messageId = _messageIdArray[indexPath.row];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
    [self  presentViewController:nav animated:YES completion:nil];
    
}

-(void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
